package com.nexgo.payment.action.scan;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.nexgo.payment.R;
import com.nexgo.payment.action.Action;
import com.nexgo.payment.action.ActionResult;
import com.nexgo.payment.action.ActionStep;
import com.nexgo.payment.action.OnActionListener;
import com.nexgo.payment.constrant.SysConstant;
import com.nexgo.payment.databases.Repository;
import com.nexgo.payment.entity.TransData;
import com.nexgo.payment.ui.BaseFragment;
import com.nexgo.payment.util.UIUtils;
import com.nexgo.payment.widget.TitleBuilder;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 创建日期:2018/1/16 16:35
 * 创建人： wzp
 * @version
 * 修改人： wzp
 * 类说明;
 */
public class BccScanOrderFragment extends BaseFragment implements Action<ActionStep>, BccScanOrderContract.View,BaseFragment.DownTimerListener{
//public class BccScanFragment extends AbsTransActivity<BccScanContract.Presenter> implements Action<ActionStep>, BccScanContract.View {


    @BindView(R.id.urlImage)
    ImageView iv_urlImage;
    @BindView(R.id.btn_confirm)
    Button btnConfirm;
    @BindView(R.id.btn_cancel)
    Button btnCancel;
    @BindView(R.id.title_tv_right)
    TextView tvTitleRight;

    private static final String TRANS_DATA = "trans_data";
    private BccScanOrderContract.Presenter mPresenter;
    private OnActionListener mListener;
    private Unbinder unbinder;

    public BccScanOrderFragment() {
    }

    public static BccScanOrderFragment newInstance(TransData transData) {
        BccScanOrderFragment fragment = new BccScanOrderFragment();
        Bundle args = new Bundle();
        args.putParcelable(TRANS_DATA, transData);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TransData transData = null;
        if (getArguments() != null) {
            transData = getArguments().getParcelable(TRANS_DATA);
        }
        new BccScanOrderPresenter(this, Repository.getInstance(),transData);
    }

    @Override
    protected View initView(LayoutInflater inflater, ViewGroup container) {
        View view = inflater.inflate(R.layout.fragment_bcc_scan_order, container, false);
        unbinder = ButterKnife.bind(this, view);

        initTitle(view);

        return view;
    }

    //初始化标题栏
    private void initTitle(View view) {
        new TitleBuilder(view)
                .setExternalTitleBgRes(R.drawable.bg_blue_icon)
                .setTitleText(getString(R.string.scan_order_scan))
                .build();
    }

    @OnClick({R.id.btn_cancel,R.id.btn_confirm})
    public void onClick(View view){
        if (UIUtils.isDoubleClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.btn_confirm:
//                mPresenter.start();
                if (mListener != null) {
                    mListener.setActionResult(new ActionResult(this, ActionResult.SUCC, null));
                }
                break;
            case R.id.btn_cancel:
                if (mListener != null) {
                    mListener.setActionResult(new ActionResult(this, ActionResult.ERR_CANCEL, null));
                }
                break;

            default:
                break;
        }
    }
    @Override
    protected void initEvent() {
    }

    @Override
    protected void initData() {
        //启动计时器
        setDownTimer(SysConstant.TIME_OUT_60, this);
    }

    @Override
    public void onResume() {
        super.onResume();
        mPresenter.start();
//        mPresenter.showOnScanNextAndRestart();
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnActionListener) {
            mListener = (OnActionListener) context;
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        mPresenter.destroy();
    }

    @Override
    public ActionStep getActionStep() {
        return ActionStep.SHOW_QRCODE;
    }



    @Override
    public void onDestroyView() {
        super.onDestroyView();
        cancelDownTimer();
        unbinder.unbind();
        mPresenter.destroy();
    }

    @Override
    public void showCountTime(int countTime) {
        setDownTimer(countTime + 1, this);
    }

    @Override
    public void setPresenter(BccScanOrderContract.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public void onTick(long secondsUntilFinished) {
        if (!isActive()) {
            return;
        }
        tvTitleRight.setVisibility(View.VISIBLE);
        tvTitleRight.setText(String.valueOf(secondsUntilFinished));
    }

    @Override
    public void onFinish() {
        if (isActive()) {
            tvTitleRight.setText("0");
        }
        if (mListener != null) {
            mListener.setActionResult(new ActionResult(BccScanOrderFragment.this, ActionResult.ERR_TIMEOUT, getString(R.string.click_scan_query_time_out)));
        }
    }

    @Override
    public boolean isActive() {
        return isAdded();
    }

    @Override
    public void showQrCode(Bitmap m) {
        iv_urlImage.setImageBitmap(m);
    }
}
